package com.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fire.api.MerchantService;
import com.fire.api.dto.MerchantDTO;
import com.fire.common.domain.BusinessException;
import com.fire.common.domain.CommonErrorCode;
import com.fire.common.util.PhoneUtil;
import com.fire.common.util.StringUtil;
import com.fire.convert.MerchantConverter;
import com.fire.entity.Merchant;
import com.fire.mapper.MerchantMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author fire
 * @date 2021年07月09日18:08
 */
@DubboService
public class MerchantServiceImpl implements MerchantService {

    @Autowired
    private MerchantMapper merchantMapper;
    @Override
    public MerchantDTO findMerchantById(long id) {
        Merchant merchant =  merchantMapper.selectById(id);
        MerchantDTO merchantDTO = new MerchantDTO();
        merchantDTO.setId(merchant.getId());
        merchantDTO.setUsername(merchant.getUsername());
        return  merchantDTO;
    }

    @Override
    public MerchantDTO createMerchant(MerchantDTO merchantDTO) throws BusinessException {
//          校验参数合法性
        if (merchantDTO==null){
            throw new BusinessException(CommonErrorCode.E_300009);
        }
        if (StringUtil.isBlank(merchantDTO.getMobile())){
            throw new BusinessException(CommonErrorCode.E_100112);
        }
//        校验手机号格式
        if (PhoneUtil.isMatches(merchantDTO.getMobile())){
            throw new BusinessException(CommonErrorCode.E_100109);
        }
//        校验手机号的唯一性
//        根据手机号查询商户表，存在则说明手机号已存在
        Integer count = merchantMapper.selectCount(new LambdaQueryWrapper<Merchant>().eq(Merchant::getMobile, merchantDTO.getMobile()));
        if (count>0){
            throw new BusinessException(CommonErrorCode.E_100113);
        }

//        调用mapper向数据库写入数据
//        Merchant merchant = new Merchant();
//        merchant.setMobile(merchantDTO.getMobile());
//        ...写入其他属性  利用方法MapStruct
        Merchant merchant = MerchantConverter.INSTANCE.dto2entity(merchantDTO);
        //审核状态为0-未审核
        merchant.setAuditStatus("0");
        merchantMapper.insert(merchant);
//        将dto中写入新增的商户的id
//        merchantDTO.setId(merchant.getId());
//        将entity转化为dto

        MerchantDTO merchantDTO1 = MerchantConverter.INSTANCE.entity2dto(merchant);

        return merchantDTO;
    }
    @Transactional
    @Override
    public void applyMerchant(Long merchantId, MerchantDTO merchantDTO) throws BusinessException {
        if (merchantId==null || merchantDTO==null){
            throw new BusinessException(CommonErrorCode.E_300009);
        }
//        校验商户ID合法性
        Merchant merchant = merchantMapper.selectById(merchantId);
        if (merchant==null){
            throw new BusinessException(CommonErrorCode.E_200002);
        }

//       将必要参数设置到entity
        merchantDTO.setId(merchantId);
        merchantDTO.setMobile(merchant.getMobile());//资质申请的时候手机号不能更改，
        merchantDTO.setAuditStatus("1");//表示已申请，待审核
        merchantDTO.setTenantId(merchant.getTenantId());

        //        将DTO转为entity
        Merchant entity = MerchantConverter.INSTANCE.dto2entity(merchantDTO);
        merchantMapper.updateById(entity);
    }
}
